const app = getApp();
Page({
	/**
	 * 页面的初始数据
	 */
	data: {
    cdnUrl: '',
		bodyHeight: 0,
		isHiddenrefuseOrderReason: true,
		isHiddencancelOrderReason: true,
		/* 自定义导航栏参数 */
		loading: false,
		color: '#000000',
		background: '#FFFFFF',
		show: true,
		animated: false,
		back: true,
		title: "扫码入库",
		/* 订单筛选 */
		status: 2,//0=待接单,1=待上门,2=待入库,3=已完成
		orderList:[],
    checkAll: false,
    count: 0,
    totalPrice: 0
	},
  checkAll: function(){
    var that = this;
    var isCheck = !that.data.checkAll;
    that.setData({
      checkAll: isCheck
    })
    that.data.orderList.forEach(function( item ,index){
      that.setData({
        ['orderList['+index+'].checked']: isCheck
      })
    })
    that.total();
  },
  check:function(e){
    var that = this;
    var id = e.currentTarget.dataset.id;
    that.data.orderList.forEach(function( item ,index){
      if( item.id == id){
        var isCheck = !item.checked;
        that.setData({
          ['orderList['+index+'].checked']: isCheck
        })
      }
    })
   that.total();
  },
  /* 计算选中的订单和总收入计算*/
  total: function(){
    var that = this;
    var count = 0;
    var totalPrice = 0;
    that.data.orderList.forEach(function( item ,index){
      if( item.checked ==true ){
        count++;
        totalPrice += parseFloat(item.real_estimate_price);
      }
    })
    that.setData({
      count: count,
      totalPrice: totalPrice
    })
    if(count==that.data.orderList.length && count!=0){
      that.setData({
        checkAll: true
      })
    }else{
      that.setData({
        checkAll: false
      })
    }
  },
  
	orderList: function(status){
		var that = this;
		app.request('/api/timetable.user/orderList', {status: status}, function (data, ret) {
      if(data.length==0){
        app.error('暂无需入库订单');
      }
		  that.setData({
		    orderList: data,
		  });
		}, function (data, ret) {
		  app.error('服务器错误');
		});
	},
	
	onLoad: function () {
	  var that = this;
		that.setData({
			cdnUrl: app.cdnUrl,
		});
    that.orderList(that.data.status);
	},
	onShow: function (){
		var that = this;
		var bodyHeight = (667-160)*750/app.globalData.windowWidth*(app.globalData.windowHeight/667);
		that.setData({
		  bodyHeight: bodyHeight,
		});
		if (app.globalData.userInfo) {
		  that.setData({ 
		    userInfo: app.globalData.userInfo,
		  });
		}else{
			app.check(that);
		}
		
	},
	onPullDownRefresh: function () {
		this.orderList(this.data.status);
	},
	onReachBottom: function () {},
	
	onShareAppMessage: function () {
	  return {
	    title: '',
	    desc: '',
	    path: '/page/index/index'
	  }
	},
	cancelOrderModalHidden: function(){
		var that = this;
		that.setData({
			isHiddencancelOrderReason: true,
		})
	},
  /* 订单入库*/
  storageOrder: function(e){
    var that = this;
    var ids = [];
    that.data.orderList.forEach(function( item ,index){
      if( item.checked ==true ){
        ids.push(item.id);
      }
    })
    if(ids.length==0){
      app.error("请选择订单");
      return false;
    }
    /* 拉起扫一扫*/
    wx.scanCode({
      onlyFromCamera: true,
      success (res) {
        console.log(res)
        /* 回收站id 从二维码中获取*/
        var pack_station_id = parseInt(res.result);
        if( isNaN(pack_station_id)){
          app.error("二维码错误");
          return false;
        }
        app.request('/api/timetable.user/storageOrder', {id: ids, pack_station_id: pack_station_id}, function (data, ret) {
          app.error(ret.msg);
        	that.orderList(that.data.status);
          that.setData({
            checkAll: false,
            count: 0,
            totalPrice: 0
          })
        }, function (data, ret) {
          app.error(ret.msg);
        	return false;
        });
      },
      fail (){
        app.error("入库失败，请重试");
        return false;
      }
    })
  }
})